﻿var flashObj = null;
$(function(){
	initFlex();
	$("#btnSave").click(function(){
		if($("#cXm").val() == "") {
			alert("姓名不能为空");return;
		}
		if(!confirm("确定要保存该信息吗？"))return;
		var gcSave = new AjaxProxy();
		gcSave.addParm("cUsername",$("#cXm").val());
		gcSave.addParm("cSex",$("#sex_1").attr("checked",true)?"1":"2");
		gcSave.addParm("cQm",$("#cQm").val());
		gcSave.addParm("cTaobao",$("#cWW").val());
		gcSave.addParm("cQq",$("#cQQ").val());
		gcSave.addParm("cDpdz",$("#cAddr").val());
		gcSave.invoke("user.saveUserInfo",false);
		var flag = gcSave.getString("PN_RET");
		if(flag != 0){
			alert(gcSave.getString("PC_MSG"));
		}else{
			alert("信息保存成功!");
		}
	});
	
	$("#btnPass").click(function(){
		if($("#oldpass").val() == "") {
			alert("请输入原始密码");
			$("#oldpass").focus();
			return;
		}
		
		if($("#newpass").val() == "") {
			alert("新密码不能为空");
			$("#newpass").focus();
			return;
		}
		
		if($("#newpass").val().length < 6) {
			alert("密码不能小于6位");
			$("#newpass").focus();
			return;
		}
		
		if($("#newpass").val() != $("#newpass2").val()) {
			alert("确认密码不正确!");
			$("#newpass2").focus();
			return;
		}
		
		if(!confirm("确定要修改密码吗？"))return;
		
		var gcPass = new AjaxProxy();
		gcPass.addParm("oldPass",$("#oldpass").val());
		gcPass.addParm("newPass",$("#newpass").val());
		gcPass.invoke("user.saveUserPass",false);
		var flag = gcPass.getString("PN_RET");
		if(flag != 0){
			alert(gcPass.getString("PC_MSG"));
		}else{
			alert("密码修改成功!");
		}
	});
})

function initFlex(){
	try {
		flashObj = swfobject.getObjectById("AlbumsUpload");
		flashObj.MXMLInitParams({
			url:"*.AJAX",
			jsFunction:"upload",
			filter:"*.jpg;*.png;*.gif",
			method:"post",
			miniMargin:10,
			miniSize:110,
			btnText:"选择头像",
			responseFunction:"response",
			errFunction:"errFunction",
			maxNums:1
		});
		getUserInfo();
		
	}catch(e){
		if(!flashObj.MXMLInitParams) {
			setTimeout("initFlex()",100);
		}
	}
}

function getUserInfo(){
	var gcUser = new AjaxProxy();
	gcUser.invoke("user.getUserInfo",false);
	var count = gcUser.getRowCount("P_RESULT");
	if(count == 1){
		var name = gcUser.getString("P_RESULT",0,"cUsername");
		var sex = gcUser.getString("P_RESULT",0,"cSex");
		var qq = gcUser.getString("P_RESULT",0,"cQq");
		var ww = gcUser.getString("P_RESULT",0,"cTaobao");
		var gxqm = gcUser.getString("P_RESULT",0,"cQm");
		var addr = gcUser.getString("P_RESULT",0,"cDpdz");
		var tx = gcUser.getString("P_RESULT",0,"cTx");
		$("#cXm").val(name==null?"":name);
		sex==1?$("#sex_1").attr("checked",true):$("#sex_2").attr("checked",true);
		$("#cQQ").val(qq==null?"":qq);
		$("#cWW").val(ww==null?"":ww);
		$("#cQm").val(gxqm==null?"":gxqm);
		$("#cAddr").val(addr==null?"":addr);
		
		flashObj.MXMLSetParam("SERVICENAME","user.uploadUserHeader");
		flashObj.MXMLSetParam("nUserid",$("#userId").val());
		if(tx != null) {
			flashObj.MXMLInitImage("../../images/header/"+tx+"?key="+new Date().getUTCMilliseconds());
		}
	}
}

function response(response){
	alert(response);
}

function upload(){
	flashObj.MXMLInitImage("../../images/header/"+$("#userId").val()+".bin?key="+new Date().getUTCMilliseconds());
	$("#userHeader", window.parent.document).attr("src","../../images/header/"+$("#userId").val()+".bin?key="+new Date().getUTCMilliseconds());
	alert("封面照片上传成功!");
}

function errFunction(err){
	alert(err);
}

